package com.mmc.delay.config;

import com.mmc.delay.api.JobHandle;
import com.mmc.delay.api.impl.DelayProducer;
import com.mmc.delay.exception.DelayException;
import com.mmc.delay.interceptor.SendInterceptor;

import java.util.*;

public class DelayCoreConfig {


    /**
     * 指定执行定时任务的ip
     */
    private static String timerIp;

    /**
     * delay 分为几个sortset存储
     */
    private static Integer partitionCount = 3;

    /**
     * ready 批量处理的数量
     */
    private static Integer readyQueueBatchSize = 20;

    /**
     * ready 没有消息的时候pop操作等待的时间
     */
    private static Long readyNotMessageWaitTime = 60 * 10 * 1000L;

    /**
     * delay 批量处理数量
     */
    private static Long delayBucketBatchSize = 20L;

    /**
     * delay 没有消息的时候pop操作等待的时间
     */
    private static Long delayNotMessageWaitTime = 60 * 10 * 1000L;


    /**
     * 拦截器
     *
     * @return
     */
    private static List<SendInterceptor> sendInterceptor;

    private static final Map<String, TopicConfig> TOPICS_CONFIG = new HashMap<>();

    private static final Map<String, JobHandle> JOB_HANDLE_MAP = new HashMap<>();

    private static final Set<String> stopTopics = new HashSet<>();

    public static Long getDelayBucketBatchSize() {
        return delayBucketBatchSize;
    }

    public static void setDelayBucketBatchSize(Long delayBucketBatchSize) {
        DelayCoreConfig.delayBucketBatchSize = delayBucketBatchSize;
    }

    public static Integer getReadyQueueBatchSize() {
        return readyQueueBatchSize;
    }

    public static Long getReadyNotMessageWaitTime() {
        return readyNotMessageWaitTime;
    }

    public static void setReadyNotMessageWaitTime(Long readyNotMessageWaitTime) {
        DelayCoreConfig.readyNotMessageWaitTime = readyNotMessageWaitTime;
    }

    public static void setReadyQueueBatchSize(Integer readyQueueBatchSize) {
        DelayCoreConfig.readyQueueBatchSize = readyQueueBatchSize;
    }

    public static Integer getPartitionCount() {
        return partitionCount;
    }

    public static void setPartitionCount(Integer partitionCount) {
        DelayCoreConfig.partitionCount = partitionCount;
    }


    public static String getTimerIp() {
        return timerIp;
    }

    public static void setTimerIp(String timerIp) {
        DelayCoreConfig.timerIp = timerIp;
    }

    public static List<SendInterceptor> getSendInterceptor() {
        return sendInterceptor;
    }

    public static void setSendInterceptor(List<SendInterceptor> sendInterceptor) {
        DelayCoreConfig.sendInterceptor = sendInterceptor;
    }

    public static Long getDelayNotMessageWaitTime() {
        return delayNotMessageWaitTime;
    }

    public static void setDelayNotMessageWaitTime(Long delayNotMessageWaitTime) {
        DelayCoreConfig.delayNotMessageWaitTime = delayNotMessageWaitTime;
    }

    public static void setTopicConfig(String topic, TopicConfig topicConfig) {
        TOPICS_CONFIG.put(topic, topicConfig);
    }


    public static Set<String> topicList() {
        return JOB_HANDLE_MAP.keySet();
    }

    public static Set<String> getStopTopics() {
        return stopTopics;
    }


    public static void addStopTopic(String topic) {
        stopTopics.add(topic);
    }

    public static void removeStopTopic(String topic) {
        stopTopics.remove(topic);
    }


    public static JobHandle getJobHandle(String topic) {
        return JOB_HANDLE_MAP.get(topic);
    }

    public static TopicConfig getTopicConfig(String topic) {
        TopicConfig topicConfig = TOPICS_CONFIG.get(topic);
        synchronized (DelayProducer.class) {
            if (topicConfig == null) {
                topicConfig = new TopicConfig();
                setTopicConfig(topic, topicConfig);
            }
        }
        return topicConfig;
    }

    public synchronized static void addJobHandle(String topic, JobHandle jobHandle) {
        JobHandle localJobHandle = JOB_HANDLE_MAP.get(topic);
        if (localJobHandle != null) {
            throw new DelayException("topic已经添加过，不能重复添加");
        }
        JOB_HANDLE_MAP.put(topic, jobHandle);
    }
}
